package com.coincalf.chan.websocket;

import com.coincalf.chan.websocket.vo.ResponseEntity;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.tio.core.Aio;
import org.tio.server.ServerGroupContext;

import java.util.Optional;

/**
 * @author qiang
 */
public class MessageHelper {

    public static void broadcast(String destination, Object message) {
        ServerGroupContext serverGroupContext = ChannelWebsocketStarter.getServerGroupContext();
        if (Optional.ofNullable(serverGroupContext).isPresent()) {
            Aio.sendToGroup(serverGroupContext, destination, new ResponseEntity()
                    .setId(serverGroupContext.getId())
                    .setSubbed(destination)
                    .put("data", message)
                    .build());
        }
    }

}
